import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '@/views/registerlogin/LoginPage.vue'
import ForgotPwdPage from '@/views/registerlogin/ForgotPwd.vue'
import Layout from '@/views/layout/IndexPage.vue'
import HomePage from '@/views/home/HomePage.vue'
import ChatPage from '@/views/home/chat.vue'
import MePage from '@/views/my/IndexPage.vue'
import ProfilePage from '@/views/my/ProfilePage.vue'
import ContactPage from '@/views/contact/IndexPage.vue'
import TalkPage from '@/views/talk/IndexPage.vue'
import SettingsPage from '@/views/my/SettingsPage.vue'
import SearchPage from '@/views/search/IndexPage.vue'
import ContactApplyPage from '@/views/search/ContactApplyPage.vue'
import ContactDetailPage from '@/views/contact/ContactDetailPage.vue'
import FriendReplyPage from '@/views/contact/FriendReplyPage.vue'
import ForgotPwd from '@/views/registerlogin/ForgotPwd.vue'
import { useUserStore } from '@/stores/user'
import FriendCirclePage from '@/views/talk/FriendCirclePage.vue'
import AddFriendCirclePage from '@/views/my/AddFriendCirclePage.vue'
import MyFriendCircle from '@/views/my/MyFriendCircle.vue'
import NotifyPage from '@/views/home/NotifyPage.vue'
import callVedio from '@/views/my/callVideo.vue'
import VideoPage from '@/views/video/IndexPage.vue'
import createGroup from '@/views/group/createGroup.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: HomePage
        },
        {
          path: '/my',
          component: MePage
        },
        {
          path: '/contact',
          component: ContactPage
        },
        {
          path: '/talk',
          component: TalkPage
        }
      ]
    },
    {
      path: '/login',
      component: LoginPage
    },
    {
      path: '/forgotpwd',
      component: ForgotPwdPage
    },
    {
      path: '/chat',
      component: ChatPage
    },
    {
      path: '/profile',
      component: ProfilePage
    },
    {
      path: '/settings',
      component: SettingsPage
    },
    {
      path: '/search',
      component: SearchPage
    },
    {
      path: '/apply',
      component: ContactApplyPage
    },
    {
      path: '/contact/:id',
      component: ContactDetailPage
    },
    {
      path: '/reply',
      component: FriendReplyPage
    },
    {
      path: '/forgotpwd',
      component: ForgotPwd
    },
    {
      path: '/friend-cicle',
      component: FriendCirclePage
    },
    {
      path: '/my-friend-circle',
      component: MyFriendCircle
    },
    {
      path: '/add-friend-circle',
      component: AddFriendCirclePage
    },
    {
      path: '/notify',
      component: NotifyPage
    },
    {
      path: '/call',
      component: callVedio
    },
    {
      path: '/play-circle',
      component: VideoPage
    },
    {
      path: '/createGroup',
      component: createGroup
    }
  ]
})
router.beforeEach(async (to, from) => {
  const store = useUserStore()
  if (
    // 检查用户是否已登录
    !store.token &&
    store.token === '' &&
    // ❗️ 避免无限重定向
    to.path !== '/login'
  ) {
    // 将用户重定向到登录页面
    return { path: '/login' }
  }
})
export default router
